﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAC.sqlHelper;
using SAC.Helper;
using SAC.DB2;

namespace DAL
{
    public class DALPubMehtod
    {
        string errMsg = "";
        string rtDBType = "";   //实时数据库
        string rlDBType = "";   //关系数据库

        StrHelper sh = new StrHelper();

        /// <summary>
        /// 初始化函数
        /// </summary>
        /// <returns></returns>
        private void init()
        {
            rtDBType = IniHelper.ReadIniData("RTDB", "DBType", null);
            rlDBType = IniHelper.ReadIniData("RelationDBbase", "DBType", null);
        }

        /// <summary>
        /// 获得班值信息
        /// </summary>
        /// <returns></returns>
        public string[] DutyValue(string date)
        {
            object obj=null;

            string[] args = new string[3];
            string[] sqls = new string[3];

            sqls[0] = "SELECT 班组编号 FROM 排班表 WHERE (结束时间 = '" + date + " 7:59:59')";

            sqls[1] = "SELECT 班组编号 FROM 排班表 WHERE (结束时间 = '" + date + " 15:59:59')";

            sqls[2] = "SELECT 班组编号 FROM 排班表 WHERE (结束时间 = '" + date + " 23:59:59')";

            for (int i = 0; i < 3; i++)
            {
                if (rlDBType == "SQL")
                    obj = DBsql.RunSingle(sqls[i], out errMsg);
                else if (rlDBType == "DB2")
                    obj = DBdb2.RunSingle(sqls[i], out errMsg);

                if (obj != null)
                {
                    args[i] = obj.ToString();
                }
            }

            return args;
        }

        /// <summary>
        /// 数据条数
        /// </summary>
        /// <returns></returns>
        public int GetCount(string repName, int unit)
        {
            int count = 0;

            string sql = "";

            sql = "SELECT count(*) FROM T_Sheet_SheetPara  WHERE (报表名称 = '" + repName + "') AND (机组 = " + unit + ") and 显示=1";

            if (rlDBType == "SQL")
                count = DBsql.RunRowCount(sql, out errMsg);
            else if (rlDBType == "DB2")
                count = DBdb2.RunRowCount(sql, out errMsg);

            return count;
        }

        public int GetCountCyd(string repName, int unit)
        {
            int count = 0;

            string sql = "";

            sql = "SELECT count(*) FROM T_Sheet_SheetPara  WHERE (报表名称 = '" + repName + "') AND (机组 = " + unit + ") and 显示=1";//

            if (rlDBType == "SQL")
                count = DBsql.RunRowCount(sql, out errMsg);
            else if (rlDBType == "DB2")
                count = DBdb2.RunRowCount(sql, out errMsg);

            return count;
        }

        public int GetCountkey(string repName, int unit)
        {
            int count = 0;
            string sql = "";
            object obj=null;

            if (rlDBType == "SQL")
            {
                sql = "select top 1 参数key from T_sheet_sheetPara WHERE (报表名称 = '" + repName + "') and  参数key !=0 order by 参数key desc";
                obj = DBsql.RunSingle(sql, out errMsg);
            }
            else if (rlDBType == "DB2")
            {
                sql = "Select 参数key FROM T_sheet_sheetPara WHERE (报表名称 = '" + repName + "') and  参数key !=0 order BY 参数key DESC FETCH FIRST 1 ROWS ONLY";
                obj = DBdb2.RunSingle(sql, out errMsg);
            }

            if (obj.ToString() != "")
                count = int.Parse(obj.ToString());

            return count;
        }

        public int GetCountData(string repName)
        {
            int count = 0;

            string sql = "";

            sql = "select count(*) from T_Sheet_SheetPara where 报表名称='" + repName + "' and  机组=1 and (参数类型='平均' or 参数类型='累计')";

            if (rlDBType == "SQL")
                count = DBsql.RunRowCount(sql, out errMsg);
            else if (rlDBType == "DB2")
                count = DBdb2.RunRowCount(sql, out errMsg);

            return count;
        }
        /// <summary>
        /// 获取某数组最大值或最小值
        /// </summary>
        /// <param name="arr">数组</param>
        /// <param name="type">值类型(大,小)</param>
        /// <returns></returns>
        public string GetMaxOrMixStr(string[] arr, string type)
        {
            string[] str;

            if (arr != null)
                str = sh.arraySort(arr);
            else
                return "";

            if (type == "大")
                return arr[0];
            else if (type == "小")
                return arr[arr.Length - 1];
            else
                return "/";
        }

        public string GetMaxOrMixStr(string[,] arr, string type)
        {
            return null;
        }

        //机组轮转
        public string GetMaxOrMixStr(string str, string type)
        {
            if (str != "" && str != null)
            {
                string[] arr;

                str = str.TrimEnd(',');

                if (str.Contains(','))
                    arr = str.Split(',');
                else
                {
                    arr = new string[1];
                    arr[0] = str;
                }

                if (arr != null)
                    arr = sh.arraySort(arr);
                else
                    return "";

                if (type == "大")
                    return arr[0];
                else if (type == "小")
                    return arr[arr.Length - 1];
                else
                    return "/";
            }
            else
                return "&nbsp;";


        }

        /// <summary>
        /// 拆解公式
        /// </summary>
        /// <param name="month">公式</param>
        /// <param name="paras">公式参数</param>
        /// <param name="paraID">ID</param>
        /// <param name="value">value</param>
        /// <returns></returns>
        public string retMon(string month, string paras, string[] paraID, string[] value)
        {
            if (paras != "")
            {
                string[] para = null;

                if (paras.Contains(','))
                    para = paras.Split(',');
                else
                {
                    para = new string[1];
                    para[0] = paras;
                }

                for (int i = 0; i < para.Length; i++)
                {
                    for (int j = 0; j < paraID.Length; j++)
                    {
                        if (paraID[j] == para[i])
                        {
                            month = month.Replace(para[i], value[j]);
                        }
                    }
                }
            }

            return month;
        }

        /// <summary>
        /// 拆解全厂公式
        /// </summary>
        /// <param name="month">公式</param>
        /// <param name="paras">公式参数</param>
        /// <param name="paraID">ID</param>
        /// <param name="value">value</param>
        /// <returns></returns>
        public string retMon(string month, string paras, string[] paraID, string[] value, string[] pabID, string[] pabVal)
        {
            if (paras != "")
            {
                string[] para = null;

                if (paras.Contains(','))
                    para = paras.Split(',');
                else
                {
                    para = new string[1];
                    para[0] = paras;
                }
                for (int i = 0; i < para.Length; i++)
                {
                    for (int j = 0; j < paraID.Length; j++)
                    {
                        if (paraID[j] != null)
                        {
                            if (paraID[j] == para[i])
                            {
                                month = month.Replace(para[i], value[j]);
                            }
                        }
                    }
                }

                for (int i = 0; i < para.Length; i++)
                {
                    for (int j = 0; j < pabID.Length; j++)
                    {
                        if (pabID[j] != null)
                        {
                            if (pabID[j] == para[i])
                            {
                                month = month.Replace(para[i], pabVal[j]);
                            }
                        }
                    }

                }
            }

            return month;
        }

        ///*******************///

        public string retMon(string mon, string paras, string[] du, string unit)
        {
            string pID = "";  //参数ID
            string pVal = ""; //参数值

            string[] idVal = null;
            string[] idVal2 = null;

            if (paras != "")
            {
                string[] para = null; //公式参数数组

                if (paras.Contains(','))
                    para = paras.Split(',');
                else
                {
                    para = new string[1];
                    para[0] = paras;
                }

                for (int i = 0; i < para.Length; i++)
                {
                    for (int j = 0; j < du.Length; j++)
                    {
                        if (du[j] != null)
                        {
                            if (du[j].Contains(';'))
                            {
                                int a = int.Parse(unit) - 1;

                                idVal = du[j].Split(';');

                                if (idVal[a].Contains(','))
                                {
                                    idVal2 = idVal[a].Split(',');

                                    pID = idVal2[0].ToString();
                                    pVal = idVal2[1].ToString();

                                    if (pID == para[i])
                                        mon = mon.Replace(para[i], pVal);
                                }
                            }
                        }
                    }
                }
            }
            return mon;
        }

        public string retMon(string mon, string paras, string[] du)
        {
            string pID = "";  //参数ID
            string pVal = ""; //参数值

            string[] idVal = null;
            string[] idVal2 = null;

            if (paras != "")
            {
                string[] para = null; //公式参数数组

                if (paras.Contains(','))
                    para = paras.Split(',');
                else
                {
                    para = new string[1];
                    para[0] = paras;
                }

                for (int i = 0; i < para.Length; i++)
                {
                    for (int j = 0; j < du.Length; j++)
                    {
                        if (du[j] != null)
                        {
                            if (du[j].Contains(';'))
                            {
                                idVal = du[j].Split(';');

                                for (int k = 0; k < idVal.Length; k++)
                                {
                                    if (idVal[k] != "" && idVal[k].Contains(','))
                                    {
                                        idVal2 = idVal[k].Split(',');

                                        pID = idVal2[0].ToString();
                                        pVal = idVal2[1].ToString();

                                        if (pID == para[i])
                                            mon = mon.Replace(para[i], pVal);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return mon;
        }
    }
}
